<template>
  <BasicModal
    v-bind="$attrs"
    title="教育实践评分"
    width="520px"
    @ok="handleSubmit"
    @register="registerModal"
  >
    <BasicForm @register="registerForm" />
  </BasicModal>
</template>

<script lang="ts" setup>
  import { ref } from "vue";
  import { BasicModal, useModalInner } from "/@/components/Modal";
  import { BasicForm, useForm } from "/@/components/Form";
  import { useMessage } from "/@/hooks/web/useMessage";
  import { scoreFormSchema } from "/@/views/default/practical/practical.data";
  import {
    setLectureNoteScore,
    setLessonPlanScore,
    setTeacherMoralScore,
    setTeachingStudyScore,
  } from "/@/api/core/internInfo";
  import { setLeaderWorkScoreApi } from "/@/api/core/leaderWork";
  import { PracTypeEnum } from "/@/enums/typeEnum";
  import { setTeachingRecordScoreApi } from "/@/api/core/teachingRecord";
  import { defaultModalFormSettings } from "/@/hooks/core/useDefault";

  const params = ref<any>({});
  const target = ref("");

  const [registerModal, { closeModal, changeLoading, changeOkLoading }] = useModalInner((data) => {
    params.value = data.params;
    target.value = data.target;
    setFieldsValue(data.score);
  });

  const [registerForm, { validate, setFieldsValue }] = useForm({
    labelWidth: 140,
    schemas: scoreFormSchema,
    ...defaultModalFormSettings,
  });

  const { createMessage } = useMessage();

  async function handleSubmit() {
    try {
      const value = await validate();
      changeLoading(true);
      changeOkLoading(true);
      switch (target.value) {
        case PracTypeEnum.LectureNote:
          await setLectureNoteScore(params.value.userId, value.schScore, value.univScore);
          createMessage.success("听课记录评分成功");
          break;
        case PracTypeEnum.LessonPlan:
          await setLessonPlanScore(params.value.userId, value.schScore, value.univScore);
          createMessage.success("实习教案评分成功");
          break;
        case PracTypeEnum.TeachingStudy:
          await setTeachingStudyScore(params.value.userId, value.schScore, value.univScore);
          createMessage.success("教研活动评分成功");
          break;
        case PracTypeEnum.TeacherMoral:
          await setTeacherMoralScore(params.value.userId, value.schScore, value.univScore);
          createMessage.success("师德表现评分成功");
          break;
        case PracTypeEnum.LeaderWork:
          await setLeaderWorkScoreApi(params.value.userId, value.schScore, value.univScore);
          createMessage.success("班主任实习评分成功");
          break;
        case PracTypeEnum.TeachingRecord:
          await setTeachingRecordScoreApi(
            params.value.userId,
            params.value.recordId,
            value.schScore,
            value.univScore,
          );
          createMessage.success("课堂教学评分成功");
          break;
      }
      closeModal();
    } finally {
      changeLoading(false);
      changeOkLoading(false);
    }
  }
</script>
